package com.asura.app.library.glide;

import android.content.Context;
import android.os.StatFs;

import com.asura.app.library.BaseApplication;
import com.asura.app.library.glide.okhttp.OkHttpUrlLoader;
import com.bumptech.glide.Glide;
import com.bumptech.glide.GlideBuilder;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.load.engine.cache.DiskCache;
import com.bumptech.glide.load.engine.cache.DiskLruCacheWrapper;
import com.bumptech.glide.load.engine.cache.LruResourceCache;
import com.bumptech.glide.load.engine.cache.MemorySizeCalculator;
import com.bumptech.glide.load.model.GlideUrl;
import com.bumptech.glide.module.GlideModule;

import java.io.File;
import java.io.InputStream;

import okhttp3.OkHttpClient;

/**
 * Created by Asura on 2016/4/19.
 */
public class GlideConfiguration implements GlideModule {


    private static final long MIN_CACHE_SIZE = 5 * 1024 * 1024;
    private static final long MAX_CACHE_SIZE = 250 * 1024 * 1024;

    @Override
    public void applyOptions(final Context context, GlideBuilder builder) {
        // Apply options to the builder here.
        builder.setDecodeFormat(DecodeFormat.PREFER_RGB_565);
        MemorySizeCalculator memorySizeCalculator = new MemorySizeCalculator(context);
        int defaultBitmapPoolSize = memorySizeCalculator.getBitmapPoolSize();
        int defaultMemoryCacheSize = memorySizeCalculator.getMemoryCacheSize();
        builder.setMemoryCache(new LruResourceCache((int) (defaultMemoryCacheSize * 1.2)));
        builder.setBitmapPool(new LruBitmapPool((int) (defaultBitmapPoolSize * 1.2)));
        builder.setDiskCache(new DiskCache.Factory() {
            @Override
            public DiskCache build() {
                File cacheDir = new File(context.getCacheDir(), "image_cache");
                if (!cacheDir.exists()) cacheDir.mkdirs();
                try {
                    StatFs statFs = new StatFs(cacheDir.getAbsolutePath());
                    long available = 0;
                    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
                        available = statFs.getAvailableBytes();
                    } else {
                        available = (long) statFs.getAvailableBlocks() * statFs.getBlockSize();
                    }
                    // ImageLoadListener 10% of the available space.
                    long cacheSize = Math.max(MIN_CACHE_SIZE, Math.min(MAX_CACHE_SIZE, available / 10));
                    return DiskLruCacheWrapper.get(cacheDir, (int) cacheSize);
                } catch (IllegalArgumentException ignored) {
                }
                return null;
            }
        });
    }

    @Override
    public void registerComponents(Context context, Glide glide) {
        // register ModelLoaders here.
        OkHttpClient okHttpClient = BaseApplication.getInstance().getModuleProvider().getOkHttpClient();
        glide.register(GlideUrl.class, InputStream.class, new OkHttpUrlLoader.Factory(okHttpClient));
    }
}